<?php
/**
 * Created by PhpStorm.
 * User: administrator
 * Date: 2016/7/16 0016
 * Time: 17:53
 */

namespace Home\Controller;
use Think\Controller;

//用户中心全局控制
class PosUploadApiController extends  Controller{

    //pos拍照图片上传
    public function PosUploadImgs()
    {


        write_file_aomai_ftp('PosUploadImgs',json_encode($_REQUEST));


        $order_sn = I('order_sn');

        $image1 = I('img1_data');

        $image2 = I('img2_data');

        $month = date('m');

        $dir_head=$_SERVER['DOCUMENT_ROOT'].'/attas/pos_img/'.$month.'/';//检验存储头像用户ID文件夹是否存在
        //文件夹不存在则创建
        if(!is_readable($dir_head))
        {
            is_file($dir_head) or mkdir($dir_head,0700);// 0777，意味着最大可能的访问权
        }

//        write_file_aomai_ftp('PosUploadImgs1',$image1);

//        write_file_aomai_ftp('PosUploadImgs2',$image2);



        $pictures='';
        if(!empty($image1)){
//            write_file_aomai_ftp('PosUploadImgs1','不等于空:'.$image1.':');
            $img1_decode = base64_decode(str_replace(" ", "+", $image1));
            $image_name =   $month.'/'.  uniqid().rand(1111,9999) . ".jpg";
            file_put_contents('./attas/pos_img/'.$image_name, $img1_decode);
            $pictures.=$image_name.'|';
        }
//        else{
//            write_file_aomai_ftp('PosUploadImgs1','等于空:'.$image1.':');
//        }


        if(!empty($image2)) {
//            write_file_aomai_ftp('PosUploadImgs2','不等于空:'.$image2.':');
            $img2_decode = base64_decode(str_replace(" ", "+", $image2));
            $image_name2 = $month . '/' . uniqid() . rand(1111, 9999) . ".jpg";
            file_put_contents('./attas/pos_img/' . $image_name2, $img2_decode);
            $pictures.=$image_name2.'|';
        }
//        else{
//            write_file_aomai_ftp('PosUploadImgs2','等于空:'.$image2.':');
//        }


        $order_id=M('order_info')->where(array('order_sn'=>$order_sn))->getField('order_id');

        $picture_record= M('order_picture_record')->where(array('order_sn'=>$order_sn,'order_id'=>$order_id))->find();



        if(empty($picture_record)){
            $data['order_sn']=$order_sn;
            $data['order_id']=$order_id;
            $data['add_time']=time();
            $data['pictures']=rtrim($pictures,'|');
            M('order_picture_record')->add($data);
        }else{

            $order_picture_list = explode('|', $picture_record['pictures']);
            foreach ($order_picture_list as $key=>$value){
                @unlink ('./attas/pos_img/'.$value);

            }

            $save_data['pictures']=rtrim($pictures,'|');
            M('order_picture_record')->where(array('order_sn'=>$order_sn,'order_id'=>$order_id))->save($save_data);
        }

        echo 1;
        exit;
    }

    //
    public function pro_updata_test(){



        $exe_version=2.0;

        $download_url=_get_Host_Name().'/attas/upload/'."SublimeText.exe";

        $file_name="SublimeText.exe";

        $retData['status']=1;
        echo json_encode($retData);
        exit;

    }

    //迁移身份证图片
    public function add_card_record(){
        //address_id.log记录的id
        $address_id= file_get_contents('address_id.log');
        //address_id.log记录的address_id大的收货地址记录
        $user_address_list=M('user_address')->where(array('address_id'=>array('gt',intval($address_id))))
            ->order('address_id')->field('address_id,user_id,consignee,idcard,idcard_img,idcardanti_img')->select();



        foreach ($user_address_list as $key => $value) {

            //在身份证记录表新增一条记录
            $card_record['consignee'] = $value['consignee'];
            $card_record['id_card'] = $value['idcard'];
            $card_record['add_time'] = time();
            $card_record['user_id'] = $value['user_id'];
            $card_record['type'] = 2;
            $card_record['idcard_img']='';
            $card_record['idcardanti_img']='';
            $card_record_id=M('card_record')->add($card_record);

            //更新用户表收货地址的card_record字段的值
            M('user_address')->where(array('address_id'=>$value['address_id']))->setField('card_record_id',$card_record_id);

            //写文件，记录收货地址ID
            $this->write_file_2('address_id', $value['address_id']);
        }

        echo '操作成功';
        exit;
    }



    public function update_user_no(){

        //user_id.log记录的id
        $user_id= file_get_contents('user_id.log');
        //user_id.log记录的user_id大的用户记录
        $user_list=M('users')->where(array('user_id'=>array('gt',intval($user_id))))
            ->order('user_id')->field('user_id,nickname')->select();


        foreach ($user_list as $key => $value) {

            include_once ($_SERVER["DOCUMENT_ROOT"] . '/includes/lib_english.php');
            
            
            //把昵称的大写装换为小写
            $nickname_str=strtolower($value['nickname']);

            /* 中文转换为英文 */
            $user_nickname = pinyin($nickname_str,'utf-8');

            /* 得到转换的英文字符长度 */
            $str_count = strlen($user_nickname);

            /*如果字符长度大于6则截取6位*/
            if($str_count>6){
                $english = mb_substr($user_nickname,0,6);
            }elseif ($str_count<6){
                $bit = 6;//产生6位数的英文编号
                $x = '';
                for($i=$str_count; $i<$bit; $i++){
                    $x .= "x";
                }
                $english = $user_nickname.$x;
            }else{
                $english = $user_nickname;
            }


            $user_no =$english.$value['user_id'];


            //更新用户表的user_no字段的值
            M('users')->where(array('user_id'=>$value['user_id']))->setField('user_no',$user_no);

            //写文件，记录用户ID
            $this->write_file_2('user_id', $value['user_id']);
        }

        echo '操作成功';
        exit;


    }


    //写文件方法
    function write_file_2($file_name,$content){
        $filename =$file_name.'.log';
        $Ts=fopen($filename,"w");
        fputs($Ts,$content);
        fclose($Ts);
    }

}